/*
 * AS_DVBS.C
 *
 * autoscan module internal API implementation for DVB-S application.
 *
 * revision
 * 1. 2006.07.07	Zhengdao Li	0.01		Initialization.
 */
#include <types.h>
#include <sys_config.h>
#include <api/libpub/as_module.h>
#include <api/libpub/lib_device_manage.h>
#include "../as_dvbs.h"

/* Check whether TP Param is right or not */
BOOL dvbs_tp_sanity_check(BOOL voltage_ctrl, T_NODE *tp, S_NODE *sat)
{
    	UINT32 lnb_type = 0;
    	UINT8 pol = 0;

	if((NULL == tp) || (NULL == sat))
	{
		return FALSE;
	}

	//Check TP Frequency
    	if((tp->frq <= AS_MIN_SAT_DL_FREQUENCY) || (tp->frq >= AS_MAX_SAT_DL_FREQUENCY))
    	{
        	return FALSE;
    	}

	//If Voltage Control, then Check TP Polarity according to LNB Type
	if (voltage_ctrl)
    	{
        	if(sat->tuner1_valid)
        	{
            		lnb_type = sat->lnb_type;
            		pol = sat->pol;
        	}
        	else if(sat->tuner2_valid)
        	{
            		lnb_type = sat->tuner2_antena.lnb_type;
            		pol = sat->tuner2_antena.pol;
        	}
        
        	if ((lnb_type == LNB_CTRL_STD) || (lnb_type == LNB_CTRL_22K))
        	{
            		if (tp->pol != pol - 1)
            		{
                		return FALSE;
            		}
        	}
    	} 
    	return TRUE;
}

/* Copy Satellite Informtion to Antenna Information */
void sat2antenna(S_NODE *sat, struct ft_antenna *antenna)
{
    	struct nim_config lnb_info;
    	UINT8 lnb_id = 0;
    	struct nim_device* nim;

	if((NULL == sat) || (NULL == antenna))
	{
		return;
	}
	
    	//this is a logic sat for dvbc or dvbt, not a real one
    	if((sat->lnb_type == 0) && (sat->lnb_low == 0) && (sat->lnb_high == 0))
    	{
        	return;
    	}
		
	//Set LNB(Tuner) ID according to NIM Device Number 
    	if(2 > dev_get_device_cnt(HLD_DEV_TYPE_NIM, FRONTEND_TYPE_S))
    	{
        	lnb_id = 1;
    	}
    	else
    	{
        	//to ensure the tuner
        	if(sat->tuner1_valid == 1)
        	{
            		lnb_id = 1;
        	}
        	else if (sat->tuner2_valid == 1)
        	{
            		lnb_id = 2;
        	}
    	}

	//Get NIM Config Information according to LNB(Tuner) ID
	nim = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, (lnb_id - 1));
	dev_get_nim_config(nim,FRONTEND_TYPE_S,&lnb_info);

	//Set Antenna Information according to NIM Antenna Type(Fixed or Motor)
	//Motor Antenna
	if(lnb_info.antenna.positioner_type)
    	{
		MEMSET(antenna, 0, sizeof(struct ft_antenna));
		antenna->position = (lnb_id == 2) ?	sat->tuner2_antena.position : sat->position;
		antenna->unicable_pos= (lnb_id == 2) ?	sat->tuner2_antena.unicable_pos: sat->unicable_pos;
		antenna->longitude = sat->sat_orbit;
		antenna->lnb_type = lnb_info.antenna.lnb_type;
		antenna->lnb_high = lnb_info.antenna.lnb_high;
		antenna->lnb_low = lnb_info.antenna.lnb_low;
		antenna->positioner_type = lnb_info.antenna.positioner_type;
    	}
	//Fixed Antenna
    	else
    	{
    		//Set Antenna Information according to LNB(Tuner) ID
        	if(lnb_id == 1)
        	{
            		antenna->lnb_high = sat->lnb_high;
	            	antenna->lnb_low = sat->lnb_low;
	            	antenna->lnb_type = sat->lnb_type;
	            	antenna->longitude = sat->sat_orbit;
	            	antenna->position = sat->position;
	            	antenna->positioner_type = 0;
	            	antenna->pol = sat->pol;	
	            	antenna->DiSEqC_type = sat->DiSEqC_type;
	            	antenna->DiSEqC_port = sat->DiSEqC_port;
	            	antenna->DiSEqC11_type = sat->DiSEqC11_type;
	            	antenna->DiSEqC11_port = sat->DiSEqC11_port;
	            	antenna->k22 = sat->k22;
	            	antenna->v12 = sat->v12;
	            	antenna->toneburst = sat->toneburst;  
                    antenna->unicable_pos = sat->unicable_pos;

        	}
        	else if (lnb_id == 2)
        	{
            		antenna->lnb_high = sat->tuner2_antena.lnb_high;
	            	antenna->lnb_low = sat->tuner2_antena.lnb_low;
	            	antenna->lnb_type = sat->tuner2_antena.lnb_type;
	            	antenna->longitude = sat->sat_orbit;
	            	antenna->position = sat->tuner2_antena.position;
	            	antenna->positioner_type = 0;
	            	antenna->pol = sat->tuner2_antena.pol;
	            	antenna->DiSEqC_type = sat->tuner2_antena.DiSEqC_type;
	            	antenna->DiSEqC_port = sat->tuner2_antena.DiSEqC_port;
	            	antenna->DiSEqC11_type = sat->tuner2_antena.DiSEqC11_type;
	            	antenna->DiSEqC11_port = sat->tuner2_antena.DiSEqC11_port;
	            	antenna->k22 = sat->tuner2_antena.k22;
	            	antenna->v12 = sat->tuner2_antena.v12;
	            	antenna->toneburst = sat->tuner2_antena.toneburst;
                    antenna->unicable_pos = sat->tuner2_antena.unicable_pos;  
        	}   
    	}

    antenna->unicable_ub = lnb_info.antenna.unicable_ub;
    antenna->unicable_freq = lnb_info.antenna.unicable_freq; 
    antenna->antenna_enable = lnb_info.antenna.antenna_enable;
}

/* Calculate all kind of Scan Progress */
UINT16 dvbs_calculate_progress(struct as_module_info *info, UINT32 freq, UINT8 polar, INT32 crnum)
{
    	UINT16 progress;
    	UINT32 from = info->param.as_from;
    	UINT32 len = info->param.as_to - from;
    	BOOL voltage_ctrl = info->voltage_ctrl;
    	struct ft_antenna *antenna = &info->antenna;

    	if ((antenna->lnb_type != LNB_CTRL_22K)&&(antenna->lnb_type != LNB_CTRL_UNICABLE)
            ||(antenna->lnb_type != LNB_CTRL_UNICABLE)&& !info->dual_LNB)
    	{
        	if (crnum <= 0)
        	{
            		progress = (PROGRESS_BAND1_OVER >> 1);
            		if ((info->dual_LNB) || voltage_ctrl)
            		{
            			/* for dual-LNB or a fixed polarity condition, polar is non-sense */
            			progress <<= 1;
            		}
            		progress = (freq-from) * progress / len;					
        	}
        	else
        	{ 
            		progress = (PROGRESS_BAND1_OVER >> 1) + (PROGRESS_LEFT1_OVER >> 1) * polar / crnum;
            		/* polar here represent the current dubious transponder index */
            		if ((info->dual_LNB) || voltage_ctrl)
            		{
                		progress <<= 1;
            		}
        	}
        	if ((info->polar == 1) && (!info->dual_LNB) && (voltage_ctrl == FALSE))
        	{
            		progress += PROGRESS_HALF_WAY;
        	}
    	} 
    	else
    	{
        	if (crnum <= 0)
        	{
            		progress = (freq - from) * PROGRESS_HALF_WAY / len;
            		if (!voltage_ctrl)
            		{
                		progress >>= 1;
                		if (info->polar)
                		{
                    			progress += PROGRESS_HALF_WAY >> 1;
                		}
             		}
        	}
        	else
        	{
            		progress = PROGRESS_HALF_WAY + polar * PROGRESS_HALF_WAY_LEFT / crnum;
            		if (!voltage_ctrl)
            		{
                		progress >>= 1;
            		}
            		if (info->polar)
            		{
                		progress += PROGRESS_25_PERCENT;
            		}
        	}
        	if (antenna->k22)
        	{
            		progress += PROGRESS_HALF_WAY;
        	}
    	}
	
	return progress;
}


